﻿Imports System.IO
Imports System.Drawing.Imaging
Imports Mysoft.Map.Extensions.Json
Imports Mysoft.Map.Extensions
Imports System.Net
Imports SunSoftUtility
Imports System.Text

Public Class ShowImage
    Private ReadOnly _hotKey As New RegHotKey
    Private WithEvents _clipMBind As ClipManager
    Private _isShow As Boolean = True
    Private _isRefresh As Boolean = True
    '【页面事件】
    Private Sub ShowImage_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        '取消注册快捷键
        UnregHotKey()
        '备份数据
        _clipMBind.SaveDataToJson()
    End Sub
    Private Sub ShowImage_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        '按钮
        btnNewText.Left = Me.ClientSize.Width - btnNewText.Width
        tabFrame.Width = Me.ClientSize.Width - btnNewText.Width
        '同步剪贴板复选框
        chkKeepTrack.Left = btnNewText.Left
    End Sub
    Private Sub ShowImage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Show()
        _clipMBind = New ClipManager(tabFrame)
        _clipMBind.RestoreDataFromFile()
        '注册快捷键
        RegHotKeySet()
        AddHandler tabFrame.DoubleClick, AddressOf CloseTab
        AddHandler tabFrame.Click, AddressOf SwitchFocus
        RefreshTimer.Enabled = True
    End Sub
    '【公共函数】
    Public Function IsImgSame(ByRef aImg As Image, ByRef bImg As Image) As Boolean
        Using ms1 As New MemoryStream, ms2 As New MemoryStream
            aImg.Save(ms1, ImageFormat.Bmp)
            bImg.Save(ms2, ImageFormat.Bmp)
            If ms1.Length <> ms2.Length Then
                Return False
            Else
                Dim aBytes() As Byte = ms1.GetBuffer()
                Dim bBytes() As Byte = ms2.GetBuffer()
                For i As Integer = 0 To aBytes.Length - 1
                    If aBytes(i) <> bBytes(i) Then
                        Return False
                    End If
                Next
                Return True
            End If
        End Using
    End Function
    Private Sub HideMe()
        If _isShow Then
            Me.Hide()
            Me.TopMost = False
            RefreshTimer.Enabled = False
        Else
            Me.Show()
            Me.TopMost = True
            RefreshTimer.Enabled = True
        End If
        _isShow = Not _isShow
    End Sub
    Private Sub btnNewText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewText.Click
        '创建新签
        _clipMBind.AddText()
    End Sub

    Private Sub RefreshTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshTimer.Tick
        If _isRefresh Then
            Me.TopMost = True
        End If

        If chkKeepTrack.CheckState = CheckState.Checked Then
            _clipMBind.AddImage(Clipboard.GetImage())
        End If
    End Sub
    '----------热键处理------------
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        _hotKey.ProcessHotKey(m)
        MyBase.WndProc(m)
    End Sub

    Private Sub RegHotKeySet()
        Try
            _hotKey.Regist(Me.Handle, RegHotKey.HotkeyModifiers.Alt, Keys.H, AddressOf HideMe)
        Catch ex As System.Exception
            MsgBox("注册快捷键失败！")
        End Try
    End Sub

    Private Sub UnregHotKey()
        _hotKey.UnRegist(Me.Handle, AddressOf HideMe)
    End Sub

    '【页签事件处理】
    Private Sub tabFrame_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tabFrame.MouseUp
        If e.Button = MouseButtons.Right Then
            '先暂时暂停刷新
            RefreshTimer.Enabled = False
            Me.TopMost = False
            Dim TabPageName As String = InputBox("新标签名")
            Dim tabCtl As TabControl = CType(sender, TabControl)
            Dim page As TabPage = tabCtl.TabPages.Item(tabCtl.SelectedIndex)
            If Not String.IsNullOrEmpty(TabPageName) Then
                page.Text = TabPageName
            End If
            RefreshTimer.Enabled = True
        End If
    End Sub
    '处理双击也签
    Private Sub CloseTab(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim tabCtl As TabControl = CType(sender, TabControl)
        tabCtl.TabPages.RemoveAt(tabCtl.SelectedIndex)
    End Sub
    Private Sub SwitchFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim tabCtl As TabControl = CType(sender, TabControl)
        Dim page As TabPage = tabCtl.TabPages.Item(tabCtl.SelectedIndex)
        Dim ctl As Control = page.Controls.Item(0)
        If TypeName(ctl) = "TextBox" Then
            ctl.Focus()
        End If
    End Sub


    Private Sub _clipMBind_ReTop() Handles _clipMBind.ReTop
        _isRefresh = True
    End Sub

    Private Sub _clipMBind_StopTop() Handles _clipMBind.StopTop
        _isRefresh = False
    End Sub

End Class
Public Class ResultJson
    Private _result As Boolean
    Public Property Result() As Boolean
        Get
            Return _result
        End Get
        Set(ByVal value As Boolean)
            _result = value
        End Set
    End Property

    Private _userData As String
    Public Property UserData() As String
        Get
            Return _userData
        End Get
        Set(ByVal value As String)
            _userData = value
        End Set
    End Property
End Class